//
//  RepayScheduleListCell.swift
//  cxd4iphone
//
//  Created by hexy on 12/22/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

private struct kConstraints {
    
    static let wrapEdges: UIEdgeInsets = UIEdgeInsetsMake(xx_height(10), 0, 0, 0)
    static let margin: CGFloat         = 10
    static let topViewHeight: CGFloat  = xx_height(30)

    static let col1Width: CGFloat      = xx_width(65)
    static let col2Width: CGFloat      = (xx_screenWidth() - 2 * kConstraints.margin) / 2 - kConstraints.col1Width// + xx_width(10)
    
    static let col3Width: CGFloat      = xx_width(55)
    static let col4Width: CGFloat      = (xx_screenWidth() - 2 * kConstraints.margin) / 2 - kConstraints.col3Width// - xx_width(10)

}

class RepayPlanListCell: BaseTableViewCell {

    override func updateUI(viewModel: BaseCellViewModel) {
        
        let vm = viewModel as! RepayPlanCellViewModel
        
        
        titleLabel.text              = vm.titleLabelText
        totalNumLabel.text           = "\(vm.repayPlanModel.corpus)"
        capitalNumLabel.text         = "\(vm.repayPlanModel.corpus)"
        handleNumLabel.text          = "\(vm.repayPlanModel.fee)"
        exceptRepayTimeNumLabel.text = vm.repayPlanModel.repayDay!

        profitNumLabel.text          = "\(vm.repayPlanModel.interest)"
        punishNumLabel.text          = "\(vm.repayPlanModel.defaultInterest)"
        stateNumLabel.text           = vm.repayPlanModel.status!
        repayTimeNumLabel.text       = vm.repayPlanModel.repayDay!
    }
    
    override func makeUI() {
        
        self.accessoryType = .None
        self.backgroundColor = Define.backgroundColor
        
        let wrap = UIView(bgColor: UIColor.whiteColor())
        
        contentView.addSubview(wrap)
        wrap.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(kConstraints.wrapEdges)
        }
        
        let hLine = UIView(bgColor: Define.separatorColor)
        
        wrap.addSubview(hLine)
        hLine.snp_makeConstraints { (make) -> Void in
            
            make.left.right.equalTo(wrap)
            make.height.equalTo(0.5)
            make.top.equalTo(wrap).offset(kConstraints.topViewHeight)
        }
        
        let topView = UIView()
        
        wrap.addSubview(topView)
        topView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(wrap).offset(kConstraints.margin)
            make.right.equalTo(wrap).offset(-kConstraints.margin)
            make.top.equalTo(wrap)
            make.height.equalTo(kConstraints.topViewHeight)
        }
    
        topView.addSubview(titleLabel)
        titleLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.centerY.equalTo(topView)
        }
        
        
//        let bottomView = UIView(bgColor: xx_randomColor())
        let bottomView = UIView()
        
        wrap.addSubview(bottomView)
        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(hLine.snp_bottom).offset(xx_height(10))
            make.left.equalTo(wrap).offset(kConstraints.margin)
            make.right.equalTo(wrap).offset(-kConstraints.margin)
            make.bottom.equalTo(wrap).offset(xx_height(-10))
        }
        
//        let col1 = UIView(bgColor: xx_randomColor())
        let col1 = UIView()
        
        bottomView.addSubview(col1)
        col1.snp_makeConstraints { (make) -> Void in
            
            make.top.left.bottom.equalTo(bottomView)
            make.width.equalTo(kConstraints.col1Width)
        }
        
//        let col2 = UIView(bgColor: xx_randomColor())
                let col2 = UIView()
        
        bottomView.addSubview(col2)
        col2.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(col1.snp_right)
            make.top.bottom.equalTo(bottomView)
            make.width.equalTo(kConstraints.col2Width)
        }
        
//        let col3 = UIView(bgColor: xx_randomColor())
                let col3 = UIView()
        
        bottomView.addSubview(col3)
        col3.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(col2.snp_right)
            make.top.bottom.equalTo(bottomView)
            make.width.equalTo(kConstraints.col3Width)
        }

//        let col4 = UIView(bgColor: xx_randomColor())
                let col4 = UIView()
        
        bottomView.addSubview(col4)
        col4.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(col3.snp_right)
            make.top.bottom.equalTo(bottomView)
            make.width.equalTo(kConstraints.col4Width)
        }

        let col1Labels = [totalLabel, capitalLabel, handleLabel, exceptRepayTimeLabel]
        var last1Lb: UILabel?
        
        for label in col1Labels {
            
            col1.addSubview(label)
            label.snp_makeConstraints(closure: { (make) -> Void in
                
                make.left.equalTo(col1)
                
                if let last = last1Lb {
                    
                    make.top.equalTo(last.snp_bottom).offset(xx_height(5))
                    
                } else {
                    
                    make.top.equalTo(col1)
                    
                }
                last1Lb = label
            })
        }
        
        
        let col2Labels = [totalNumLabel, capitalNumLabel, handleNumLabel, exceptRepayTimeNumLabel]
        var last2Lb: UILabel?
        
        for label in col2Labels {
            
            col2.addSubview(label)
            label.snp_makeConstraints(closure: { (make) -> Void in
                
                make.left.equalTo(col2)
                
                if let last = last2Lb {
                    
                    make.top.equalTo(last.snp_bottom).offset(xx_height(5))
                    
                } else {
                    
                    make.top.equalTo(col2)
                    
                }
                last2Lb = label
            })
        }

        let col3Labels = [profitLabel, punishLabel, stateLabel, repayTimeLabel]
        var last3Lb: UILabel?
        
        for label in col3Labels {
            
            col3.addSubview(label)
            label.snp_makeConstraints(closure: { (make) -> Void in
                
                make.left.equalTo(col3)
                
                if let last = last3Lb {
                    
                    make.top.equalTo(last.snp_bottom).offset(xx_height(5))
                    
                } else {
                    
                    make.top.equalTo(col3)
                    
                }
                last3Lb = label
            })
        }
        
        let col4Labels = [profitNumLabel, punishNumLabel, stateNumLabel, repayTimeNumLabel]
        var last4Lb: UILabel?
        
        for label in col4Labels {
            
            col4.addSubview(label)
            label.snp_makeConstraints(closure: { (make) -> Void in
                
                make.left.equalTo(col4)
                
                if let last = last4Lb {
                    
                    make.top.equalTo(last.snp_bottom).offset(xx_height(5))
                    
                } else {
                    
                    make.top.equalTo(col4)
                    
                }
                last4Lb = label
            })
        }


    }
    
    lazy var titleLabel              = UILabel(num: "期数:  1期", fontSize: 14)


    lazy var totalLabel              = UILabel(title: "总额(元)", fontSize: 12)
    lazy var capitalLabel            = UILabel(title: "本金(元)", fontSize: 12)
    lazy var handleLabel             = UILabel(title: "手续费(元)", fontSize: 12)
    lazy var exceptRepayTimeLabel    = UILabel(title: "预计还款日", fontSize: 12)

    lazy var totalNumLabel           = UILabel(num: "34,567,890.00", fontSize: 12)
    lazy var capitalNumLabel         = UILabel(num: "0.00", fontSize: 12)
    lazy var handleNumLabel          = UILabel(num: "0.00", fontSize: 12)
    lazy var exceptRepayTimeNumLabel = UILabel(num: "2015-11-12", fontSize: 12)


    lazy var profitLabel             = UILabel(title: "利息(元)", fontSize: 12)
    lazy var punishLabel             = UILabel(title: "罚息(元)", fontSize: 12)
    lazy var stateLabel              = UILabel(title: "还款状态", fontSize: 12)
    lazy var repayTimeLabel          = UILabel(title: "还款时间", fontSize: 12)

    lazy var profitNumLabel          = UILabel(num: "0.00", fontSize: 12)
    lazy var punishNumLabel          = UILabel(num: "0.00", fontSize: 12)
    lazy var stateNumLabel           = UILabel(title: "已完成", fontSize: 12)
    lazy var repayTimeNumLabel       = UILabel(num: "2015-11-12", fontSize: 12)
    
}
